const { list, add, find, deleteRecord } = require('./data/coupon-def-items')

const base = '/shopmovie'

module.exports = [{
  url: `${base}/couponDefinitions`,
  type: 'post',
  response: config => {
    add(config.body)
    return { success: true }
  }
}, {
  url: `${base}/couponDefinitions`,
  type: 'get',
  response: config => {
    const { page, limit, name, orgUnitId, type } = config.query
    return list(page, limit, name, orgUnitId, type)
  }
}, {
  url: `${base}/couponDefinitions/[1-9][0-9]*`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      const { name, type, movieLevelId, amount, maxAmount } = config.body
      r.name = name
      r.type = type
      r.movieLevelId = movieLevelId
      r.amount = amount
      r.maxAmount = maxAmount
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/couponDefinitions/[1-9][0-9]*`,
  type: 'delete',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    deleteRecord(id)
    return { success: true }
  }
}]
